package com.huike.game.Activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.base.Game69;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import androidx.appcompat.app.AppCompatActivity;

public class GameActivity69 extends AppCompatActivity {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private GridView gv_game69;
    private ProgressDialog show;
    private RequestQueue queue;
    private Game69 json;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game69);
        queue = Volley.newRequestQueue(this);
        initView();
        initData();

    }

    private void initData() {
        show();
        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "lifeAppear", new Response.Listener<JSONObject>() {


            @Override
            public void onResponse(JSONObject jsonObject) {
                hide();

                json = new Gson().fromJson(jsonObject.toString(), Game69.class);

                gv_game69.setAdapter(new ListViewAdapter());

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                hide();
                Toast.makeText(GameActivity69.this, "数据加载失败！请检查网络。", Toast.LENGTH_SHORT).show();
            }
        });

        queue.add(request);
    }

    private void show() {
        show = ProgressDialog.show(this, "提示", "加载中...");
        show.show();
    }

    private void hide() {
        show.dismiss();
    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);

        iv_rigth_image.setVisibility(View.GONE);
        iv_left_image.setImageResource(R.drawable.tiao);
        tv_content_title.setText("旅行助手");
        gv_game69 = (GridView) findViewById(R.id.gv_game69);
        tv_left_text.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);
    }


    class ListViewAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return json.getData().size();
        }

        @Override
        public Object getItem(int position) {
            return json.getData().get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(GameActivity69.this, R.layout.gv_game69, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final Game69.DataBean bean = json.getData().get(position);

            holder.tv_game69_title.setText(bean.getName());
            holder.tv_game69_price.setText("票价￥："+bean.getPrice());

//            解析图片
            final ViewHolder finalHolder = holder;
            ImageRequest request = new ImageRequest(bean.getImage(), new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap bitmap) {
                    finalHolder.iv_game69_image.setImageBitmap(bitmap);
                }
            },0,0, ImageView.ScaleType.CENTER, Bitmap.Config.ARGB_8888,null);
            queue.add(request);

            //购买点击
            holder.tv_game69_pay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //得到点击的位置数据 //转换成字符串 保存缓存
                    String toJson = new Gson().toJson(bean);

                    //读取缓存保存
                    SharedPreferences list = getSharedPreferences("game69list", MODE_PRIVATE);
                    String data = list.getString("data", null);

                    StringBuffer buffer;
                    if(TextUtils.isEmpty(data)){
                        buffer = new StringBuffer();
                        buffer.append(toJson).append("-");
                    }else{
                        //判断是否已经添加
//                        ArrayList<Game69.DataBean> objectArrayList = new ArrayList<>();
//                        String[] split = data.split("-");
//                        for (int i = 0; i < split.length; i++) {
//                            Game69.DataBean dataBean = new Gson().fromJson(split[i], Game69.DataBean.class);
//                            objectArrayList.add(dataBean);
//
//
//                            if(objectArrayList.get(i).getName().equals(bean.getName())){
//                                Toast.makeText(GameActivity69.this, "已经添加购物车了！无需在添加", Toast.LENGTH_SHORT).show();
//                                return;
//                            }
//
//                        }

                        //添加缓存
                        buffer = new StringBuffer(data);
                        buffer.append(toJson).append("-");
                    }

                    list.edit().putString("data",buffer.toString()).commit();

                    //跳转购物车页面
                    startActivity(new Intent(GameActivity69.this,GameActivity69Shoop.class));


                }
            });

            //图片点击
            holder.iv_game69_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //使用eventbs 传递数据
                    EventBus.getDefault().postSticky(bean);
                    startActivity(new Intent(GameActivity69.this,GameActivity69desc.class));
                }
            });


            return convertView;
        }

        class ViewHolder {
            public View rootView;
            public ImageView iv_game69_image;
            public TextView tv_game69_title;
            public TextView tv_game69_pay;
            public TextView tv_game69_price;

            public ViewHolder(View rootView) {
                this.rootView = rootView;
                this.iv_game69_image = (ImageView) rootView.findViewById(R.id.iv_game69_image);
                this.tv_game69_title = (TextView) rootView.findViewById(R.id.tv_game69_title);
                this.tv_game69_pay = (TextView) rootView.findViewById(R.id.tv_game69_pay);
                this.tv_game69_price = (TextView) rootView.findViewById(R.id.tv_game69_price);
            }

        }
    }
}
